using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 失明Buff - 使目标无法准确攻���
/// </summary>
public class BlindBuff : BuffBase
{
    private float m_OriginalAccuracy;
    private float m_AccuracyReduction;

    public BlindBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is BlindData blindData)
        {
            m_AccuracyReduction = blindData.AccuracyReduction;
        }
        else
        {
            m_AccuracyReduction = 0.5f; // 默认降低50%命中���
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始命中率（这里假设有AttrComponent存储命中率）
        // m_OriginalAccuracy = Target.AttrComponent.Accuracy;
        
        // 降低命中���
        // Target.AttrComponent.SetAccuracy(m_OriginalAccuracy * (1.0f - m_AccuracyReduction));
        
        // 播放失明特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("blinded", true);
        }
        
        Logger.Log($"{Target.name} 失明了，命中率降���{m_AccuracyReduction * 100}%");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 失明期间不需要特殊处���
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("BlindBuff", $"{Target.name} 失明效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始命中���
        // if (Target.AttrComponent != null)
        // {
        //     Target.AttrComponent.SetAccuracy(m_OriginalAccuracy);
        // }
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("blinded", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Blind"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 失明效果不能叠加，只能刷新持续时���
    }
}
